<?php

declare(strict_types=1);

namespace app\service\member;

use app\model\member\MemberModel;

class MemberService
{
    /**
     * 筛选
     * @param array $param
     */
    public static function _screen(array $param): array
    {
        $map = [];
        // ID
        if ($id = $param['id'] ?? 0) {
            $map[] = ['id', '=', $id];
        }
        // 状态 (0正常 1停用)
        $status = $param['status'] ?? '';
        if (is_numeric($status)) {
            $map[] = ['status', '=', $status];
        }
        // 区域认证: 0未认证 1已认证
        $regionAttestation = $param['region_attestation'] ?? '';
        if (is_numeric($regionAttestation)) {
            $map[] = ['region_attestation', '=', $regionAttestation];
        }
        // 昵称
        if ($nickname = $param['nickname'] ?? '') {
            $map[] = ['nickname', 'like', trim($nickname) . '%'];
        }
        // 手机号
        if ($mobile = $param['mobile'] ?? '') {
            $map[] = ['mobile', 'like', trim($mobile) . '%'];
        }
        // 姓名
        if ($name = $param['name'] ?? '') {
            $map[] = ['name', 'like', trim($name) . '%'];
        }

        return $map;
    }
    /**
     * 会员列表
     * @param array $param
     */
    public function memberList(array $param): array
    {
        $map = self::_screen($param);

        $list = MemberModel::where($map)
            ->with(['info'])
            ->order('id desc')
            ->pages($param)
            ->select();

        return list_fmt($list, MemberModel::where($map)->count());
    }
    /**
     * 编辑会员信息
     * @param array $data
     * @param integer $id
     */
    public function memberEdit(array $data, int $id): void
    {
        $member = MemberModel::find($id);
        if (!$member) {
            tips('未找到会员信息');
        }
        $member->status = $data['status'];
        $member->save();
    }
}
